package parsing;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import main.ISpecificPath;
import model.Document;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import database.SQLHelper;

/**
 * The class that parse a 
 */
public class DocumentParser
{
  /**
   * Parses the 
   *
   * @param collectionPath the collection path
   * @return the list
   */
  public static List<Document> parse()
  {
    List<Document> listDocs = null;
    SAXParserFactory fabrique = SAXParserFactory.newInstance();
    SAXParser parseur;
    try
    {
      parseur = fabrique.newSAXParser();

      listDocs = SQLHelper.getDocumentsInDatabase();

      // Parse chaque document de la bdd.
      for (Document doc : listDocs)
      {
        File fichier = new File(ISpecificPath.COLLECTION_PATH + "/" + doc.getName());
        DefaultHandler gestionnaire = new DocHandler(doc);
        parseur.parse(fichier, gestionnaire);
      }
    }
    catch (ParserConfigurationException | SAXException | IOException e)
    {
      e.printStackTrace();
    }
    return listDocs;
  }
}
